//
//  HLTabBar.swift
//  HLwWeibo
//
//  Created by hqhxu on 16/2/26.
//  Copyright © 2016年 hqhxu. All rights reserved.
//

import UIKit

//相当于定义代理协议
@objc protocol HLtabBarDelegate:NSObjectProtocol {
    
    optional
    
    func tabBarDidClick(button:UIButton)
    
}


class HLTabBar: UITabBar {
    
    //相当于定义代理属性
    weak var mainTabBarDelegate:HLtabBarDelegate?
    
    /// 按钮的懒加载
    lazy var composeBtn:UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        
        self.addSubview(button)
        
        button.addTarget(self, action: "didClickcomposeButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 按钮点击事件
    func didClickcomposeButton(button:UIButton) {
        print("点击了按钮")
        //弹出一个控制器
        /*
        ？问号在方法中代表有就执行 没有就不执行
        ? 在属性这个属性可以有也可以没有
        */
        
        mainTabBarDelegate?.tabBarDidClick?(button)
    }
    
    /// 布局自控制的时候调用
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let x = self.bounds.width * 0.5
        let y = self.bounds.height * 0.5
        let width = self.bounds.width / 5
        
        composeBtn.frame = CGRect(x: 0, y: 0, width: width, height: self.bounds.height)
        
        composeBtn.center = CGPoint(x: x, y: y)
        
        
    }
    
    
    
}
